package twentytwentyone_2;

import java.util.Arrays;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2021-02-23 13:34
 * @Modified By:
 */
public class FindComplement_476 {
    public static void main(String[] args) {
        System.out.println(findComplement(7));
    }

    public static int findComplement(int num) {
        // 将num转换为二进制
        String s = Integer.toBinaryString(num);

        // 将二进制num进行取反操作
        char[] firstNumbers = s.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        for (char firstNumber : firstNumbers) {
            if (firstNumber == '1'){
                stringBuilder.append(0);
            }else {
                stringBuilder.append(1);
            }
        }
        int result = Integer.parseInt(stringBuilder.toString(), 2);
        return result;
    }
}
